from django.http import JsonResponse
from datetime import datetime
from zoneinfo import ZoneInfo

def create_response(data=None, code=200, message="Success", remarks=None,cookies= None):
    # 获取当前北京时间
    beijing_time = datetime.now(ZoneInfo('Asia/Shanghai')).isoformat()
    # # 获取服务器的当前时间
    # server_time = datetime.now().isoformat()
    response = {
        "code": code,
        "timestamp": beijing_time,
        "message": message,
        "remarks": remarks,
        "data": data
    }
    # 创建 JsonResponse 对象
    json_response = JsonResponse(response, status=code, json_dumps_params={'ensure_ascii': False})
    
    # 设置 Cookie
    if cookies:
        for key, value in cookies.items():
            json_response.set_cookie(
                key=key,
                value=value["value"],
                max_age=value.get("max_age", 3600 * 24),  # 默认过期时间为 1 天
                httponly=value.get("httponly", True),     # 默认启用 HttpOnly
                secure=value.get("secure", True),         # 默认启用 Secure
                samesite=value.get("samesite", "Lax")     # 默认启用 SameSite
            )
    return json_response